<template>
	<view>
		<view style="padding: 20rpx;">
			<u-search placeholder="搜索" v-model="search_title" @change="getFilterProductList"></u-search>
		</view>

		<view class="tab">
			<view :class="{'tab-item':true,'active':current == item.name}" v-for="(item,index) in tab_list"
				:key="item.title" @click="changeList(item,index)">
				{{item.title}}
				<view style="display: flex;flex-direction: column;align-items: center;margin-left: 3rpx;" v-if="item.sortSwitch">
					<u-icon name="arrow-up-fill" size="8" :color="item.sortArr ==='desc' && '#ccc'"></u-icon>
					<u-icon name="arrow-down-fill" size="8"  :color="item.sortArr ==='asc' && '#ccc'"></u-icon>
				</view>
			</view>
		</view>
		
		<view class="list-cell">
			<view class="item" v-for="item,index in product_list" :key="item._id" @click="goInfo(item)">
				<image class="image" :src="item.image" mode="aspectFill"></image>
				<view class="bottom">
					<view>{{item.title}}</view>
					<view style="display: flex;justify-content: space-between;">
						<view>
							<view style="color: #e83437;font-size: 28rpx;margin-top: 8rpx;">￥{{item.price}}</view>
							<view style="font-size: 22rpx;color:#ccc">库存:{{item.inventory}}</view>
						</view>
						<view @click.stop="changeCurrentInfo(item)">
							<u-icon  name="shopping-cart" size="30" color="#e83437"></u-icon>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view v-if="is_load" style="padding-top: 80rpx;">
			<u-loading-icon text="加载中..."></u-loading-icon>
		</view>
		<view v-if="product_list.length <= 0 && is_load === false" style="padding-top: 80rpx;">
			<u-empty
			        mode="car"
					text="搜索内容为空"
			        icon="https://cdn.uviewui.com/uview/empty/data.png"
			>
			</u-empty>
		</view>
		
		
		<sku-buy :show="show" :currentInfo="current_info" @close="show = false" />
	</view>
</template>

<script>
	import skuBuy from '@/components/skuBuy.vue'
	export default {
		components:{
			skuBuy
		},
		onLoad() {
			this.getFilterProductList()
		},
		data() {
			return {
				is_load:false,
				search_title: "",
				current: 'all',
				current_name:'all',
				current_sortArr:'desc',
				product_list:[],
				tab_list: [{
						title: "综合",
						name: "all",
						sortArr: "desc"
					},

					{
						title: "最新",
						name: "_add_time",

						sortArr: "desc"
					},

					{
						title: "价格",
						name: "price",
						sortArr: "desc",
						sortSwitch: true
					},

					{
						title: "销量",
						name: "sales_value",
						sortArr: "desc"
					},

				],
				show:false,
				current_info:null
			}
		},
		methods: {
			goInfo(item){
				uni.navigateTo({
					url:`/pages/product/product?product_id=${item._id}`
				})
			},
			changeCurrentInfo(item){
				console.log("发UC看",item)
				this.current_info = item
				this.show  =true
			},
			async getFilterProductList() {
				this.is_load = true
				this.product_list = []
				await vk.callFunction({
					url: '/client/pages/product/pub/get_filter_product_list',
					// title: '请求中...',
					data: {
						name:this.current_name,
						sortArr: this.current_sortArr,
						search_title:this.search_title
					},
					success:(res)=>{
						this.product_list = res.data
					},
					complete:(res)=>{
						this.is_load = false
					}
				});
				

			},
			changeList(item,index) {
				let current_sortArr = this.tab_list[index].sortArr
				if(item.sortSwitch && this.current_name === item.name){
					current_sortArr === 'desc' ?  current_sortArr = 'asc' : current_sortArr = 'desc'
					item.sortArr =current_sortArr
				}
				console.log(item.sortArr)
				this.current_name = item.name
				this.current_sortArr = item.sortArr
				this.getFilterProductList()
				this.current = item.name
			}
		}
	}
</script>

<style lang="less">
	page {
		background-color: #F7F7F7;
	}

	.tab {
		display: grid;
		padding: 0 20rpx;
		font-size: 26rpx;
		justify-content: space-around;
		grid-template-columns: repeat(4, auto);

		.tab-item {
			display: flex;
			align-items: center;
			&.active {
				color: #e83437;
			}
		}
	}
	
	.list-cell{
		display: grid;
		grid-template-columns: repeat(2, auto);
		justify-content: space-between;
		padding: 20rpx;
		box-sizing: border-box;
		.item{
			margin-bottom: 20rpx;
			border-radius: 10rpx;
			overflow: hidden;
			width: 345rpx;
			background-color: white;
			
			.image{
				width: 100%;
				height:300rpx;
			}
			
			.bottom{
				font-size: 28rpx;
				padding: 20rpx;
				box-sizing: border-box;
			}
		}
	}
</style>